package com.wondersgroup.fjzlpt.yyzyzx.dao.impl.yyzycx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzycx.YjxxDAO;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 医技信息数据访问实现层
 * <p>
 * <p>
 * Created by sanma on 2017/8/7.
 */
@Repository
public class YjxxDAOImpl  extends BaseJTDAOImpl implements YjxxDAO {

	/**
     *  分页查询医技信息
     * @param map
     * @return
     */
    @Override
    public JSONObject pageQueryYjxxAll(Map map) {
        JSONObject sqlObj = this.getQueryPageSql(map);
        List list = (List)sqlObj.get("sqlParams");
        
        PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), " order by YYRQ ", list.toArray());
        return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
    }

    /**
     * 查询所有医技信息
     * @param map
     * @return
     */
	@Override
	public JSONArray queryAll(Map map) {
		JSONObject sqlObj = this.getQueryPageSql(map);
        List list = (List)sqlObj.get("sqlParams");
        
        SQLBean bean = new SQLBean(sqlObj.getString("sql")+" order by YYRQ ", list.toArray());
		return super.queryList(bean);
	}
	
	/**
	 * 分页语句拼接
	 * @param map
	 * @return
	 */
	private JSONObject getQueryPageSql(Map<String,String> map){
		JSONObject result = new JSONObject();
		
		StringBuilder sqlBuf = new StringBuilder("SELECT YJBH , YLJGMC , KSMC_1 KSMC , YYRQ , XMFLMC , XMMC , TWMC , YYSJD ,")
		        .append(" SFJJ , SYKYYS , YYYS  FROM V_ZYZX_YJXX WHERE 1 = 1 \n");
		List sqlParams = new ArrayList();
		String value = (String)map.get("YLJGDM");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and YLJGDM = ? \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("YJKSDM");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and KSDM_1 = ? \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("XMFLDM");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and XMFLDM = ? \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("XMDM");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and XMDM = ? \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("YYRQ_START");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and YYRQ >= TO_DATE(?,'yyyy-MM-dd') \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("YYRQ_END");
		if(!StringUtils.isEmpty(value)){
		    sqlBuf.append(" and YYRQ <= TO_DATE(?,'yyyy-MM-dd') \n");
		    sqlParams.add(value);
		}
		value = (String)map.get("XZQHDM");
		if(!StringUtils.isEmpty(value)){
			sqlBuf.append(" and XZQHDM like ? \n");
			sqlParams.add(value+"%");
		}
		result.put("sql", sqlBuf.toString());
		result.put("sqlParams", sqlParams);
		return result;
	}
}
